package com.ms.p2p.service.impl;


import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.dao.MemberAccountDao;
import com.ms.p2p.dao.MemberAccountRecordDao;
import com.ms.p2p.entity.account.Account;
import com.ms.p2p.entity.account.AccountRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class MemberAccountRecordServiceImpl {

	@Autowired
	MemberAccountRecordDao accountLogDao;

	@Autowired
	MemberAccountDao memberAccountDao;
	/**
	 * 记录资金记录
	 *
	 * @param account
	 *            操作之后的账户详情
	 * @param toUserId
	 *            钱给谁
	 * @param money
	 *            操作金额
	 * @param remark
	 *            记录详情
	 * @param addIp
	 * @param type
	 *            资金记录类型
	 * @param flow
	 *            资金流向 (in：收入 out：支出 frozen：冻结)
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int record( int user_id, BigDecimal money,
						  String remark, String addIp, String type, String flow) {

		Account account  = memberAccountDao.findByUserId(user_id);
		AccountRecord record = new AccountRecord();
		record.setUser_id(account.getUser_id());
		record.setType(type);
		record.setFlow(flow);
		record.setMoney(money);
		record.setTotal(account.getTotal());
		record.setBalance(account.getBalance());
		record.setFreezing_amont(account.getFreezing_amont());
		record.setCollection(account.getCollection());
		record.setRemark(remark);
		record.setAddip(addIp);
		record.setAddtime(MsDateUtils.getTime13());
		return accountLogDao.insert(record);

	}

	/**
	 * sta money
	 * */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public Map<String, Object> staticMoney(String flow ,String user_id) {
		return accountLogDao.caculateMoney(flow,user_id);
	}


	public List<Map<String, Object>> findPager(String userId, String userName,
											   String account_type, String begin_date, String end_date,
											   int pageBegin, Integer pageSize) {
		return accountLogDao.findPager(pageBegin, pageSize, userId, userName,
				account_type, begin_date, end_date);

	}

	public int findCount(String userId, String userName, String account_type,
						 String begin_date, String end_date) {
		return accountLogDao.findCount(userId, userName, account_type,
				begin_date, end_date);
	}


}
